package pl.squeaky.mvc.command;

import java.util.ArrayList;
import java.util.Arrays;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;

import pl.squeaky.mvc.MvcProperties;
import pl.squeaky.mvc.nature.MvcNature;
import pl.squeaky.mvc.nature.wizard.MvcWizard;

public class AddRemoveMvcCommand extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		System.out.println("AddRemoveMvcCommand::execute");
		
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if(selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection)selection;
			for(Object object : structuredSelection.toArray()) {
				if(object instanceof IProject) {
					IProject project = (IProject)object;
					
					toggleNature(project);
				}
			}
		}
		
		return null;
	}
	
	private void toggleNature(IProject project) {
		IProjectDescription description;
		try {
			description = project.getDescription();
		} catch (CoreException e) {
			throw new RuntimeException(e);
		}
		
		String[] natureIds = description.getNatureIds();
		String[] newNatureIds;
		
		String controllerPath = null;
		String modelPath = null;
		String viewPath = null;
		if(description.hasNature(MvcNature.NATURE_ID)) {
			ArrayList<String> list = new ArrayList<String>(Arrays.asList(natureIds));
			list.remove(MvcNature.NATURE_ID);
			newNatureIds = list.toArray(new String[list.size()]);
		} else {
			MvcWizard wizard = new MvcWizard(project);
			
			Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
			
			WizardDialog dialog = new WizardDialog(shell, wizard);
			dialog.setBlockOnOpen(true);
			dialog.create();
			if(dialog.open() == Dialog.CANCEL) {
				return;
			}
			
			//TODO: remove
			System.out.println("Finish!");
			
			controllerPath = wizard.getControllerPath().isEmpty() ? null : wizard.getControllerPath();
			modelPath = wizard.getModelPath().isEmpty() ? null : wizard.getModelPath();
			viewPath = wizard.getViewPath().isEmpty() ? null : wizard.getViewPath();
			
			try {
				project.setPersistentProperty(MvcProperties.controllerPath, controllerPath);
				project.setPersistentProperty(MvcProperties.modelPath, modelPath);
				project.setPersistentProperty(MvcProperties.viewPath, viewPath);
			} catch(CoreException e) {
				throw new RuntimeException(e);
			}
			
			newNatureIds = Arrays.copyOf(natureIds, natureIds.length + 1);
			newNatureIds[natureIds.length] = MvcNature.NATURE_ID;
			
		}
		
		description.setNatureIds(newNatureIds);
		
		try {
			project.setDescription(description, null);
		} catch (CoreException e) {
			throw new RuntimeException(e);
		}
	}

}
